package com.gzhryc.net_marketing.cloud.business.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.api.DefaultAPICodeConstants;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.api.protocol.order.CreateSupplierGoodsOrderRequest;
import com.gzhryc.net_marketing.api.protocol.order.CreateSupplierGoodsOrderResponse;
import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.business.dao.CloudGoodsPayOrderDao;
import com.gzhryc.net_marketing.cloud.business.dao.db.CloudGoodsPayOrder;
import com.gzhryc.net_marketing.cloud.business.dao.db.MarketSupplier;
import com.gzhryc.net_marketing.cloud.business.services.dto.CloudGoodsPayOrderSearch;
import com.gzhryc.net_marketing.cloud.goods.dao.db.CloudGoods;
import com.gzhryc.net_marketing.cloud.goods.services.CloudGoodsService;
import com.gzhryc.net_marketing.cloud.notice.SupplyChainAPIService;
import com.gzhryc.net_marketing.core.enums.EGoodsState;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class CloudGoodsPayOrderService {

	static Logger log = Logger.getLogger(CloudGoodsPayOrderService.class);

	public static CloudGoodsPayOrderService self() {
		return MultiDBTools.getService(MarketConstants.JDBC_KEY,CloudGoodsPayOrderService.class, true);
	}

	public static CloudGoodsPayOrderService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey, CloudGoodsPayOrderService.class, true);
	}

	CloudGoodsPayOrderDao dao;

	public CloudGoodsPayOrderService(String jdbcKey) {
		dao = new CloudGoodsPayOrderDao(jdbcKey);
	}

	public List<CloudGoodsPayOrder> findBySearch(CloudGoodsPayOrderSearch search, Integer pageNo, Integer pageLimit) {
		Conditions condition = dao.buildConditions(search);
		condition.desc("createDate");
		try {
			return dao.findByCondition(condition, pageNo, pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}
	
	public Long countBySearch(CloudGoodsPayOrderSearch search) {
		Conditions condition = dao.buildConditions(search);
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
		return 0L;
	}

	public CloudGoodsPayOrder getByTradeNo(String tradeNo){
		if(StringUtils.isNotBlank(tradeNo)){
			try {
				return dao.getById(tradeNo);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public CloudGoodsPayOrder getBySupplierTradeNo(String supplierTradeNo){
		if(StringUtils.isNotBlank(supplierTradeNo)){
			Conditions condition = new Conditions(CloudGoodsPayOrder.class);
			condition.and().is("supplierTradeNo",supplierTradeNo);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public CreateSupplierGoodsOrderResponse add(CreateSupplierGoodsOrderRequest apiReq) throws LogicException {
		CloudGoods goods = CloudGoodsService.self().getById(apiReq.getBody().getGoodsCode());
		if (goods == null) {
			throw new LogicException("商品不存在");
		}

		if (!EGoodsState.PutOnTheShelf.index().equals(goods.getState())) {
			throw new LogicException(EGoodsState.note(goods.getState()));
		}

		MarketSupplier supplier = MarketSupplierService.self().getById(goods.getSupplierId());
		if (supplier != null) {
			CloudGoodsPayOrder supplierGoodsOrder = getByTradeNo(apiReq.getBody().getProjectTradeNo());
			try {
				CreateSupplierGoodsOrderResponse tempResp = SupplyChainAPIService.self().createGoodsOrder(supplier.getUserId(), apiReq);
				if (tempResp != null) {
					if(DefaultAPICodeConstants.SUCCESS.equals(tempResp.getCode())) {
						if(supplierGoodsOrder != null) {
							if(!tempResp.getBody().getState().equals(supplierGoodsOrder.getState())
								|| !tempResp.getBody().getTradeNo().equals(supplierGoodsOrder.getSupplierTradeNo())
								|| !tempResp.getBody().getMoney().equals(supplierGoodsOrder.getMoney())
									|| !goods.getId().equals(supplierGoodsOrder.getGoodsCode())){
								CloudGoodsPayOrder updateEntity = new CloudGoodsPayOrder();
								updateEntity.setTradeNo(apiReq.getBody().getProjectTradeNo());
								updateEntity.setProjectId(apiReq.getBody().getProjectId());
								updateEntity.setProjectName(apiReq.getBody().getProjectName());
								updateEntity.setGoodsCode(goods.getId());
								updateEntity.setGoodsName(goods.getName());
								updateEntity.setSupplierId(supplier.getUserId());
								updateEntity.setSupplierName(supplier.getName());
								updateEntity.setSupplierTradeNo(tempResp.getBody().getTradeNo());
								updateEntity.setMoney(tempResp.getBody().getMoney());
								updateEntity.setState(tempResp.getBody().getState());
								updateEntity.setCreateDate(new Date());
								if (dao.updateNotNull(updateEntity) > 0) {
									return tempResp;
								}
							}else {
								return tempResp;
							}
						}else{
							supplierGoodsOrder = new CloudGoodsPayOrder();
							supplierGoodsOrder.setTradeNo(apiReq.getBody().getProjectTradeNo());
							supplierGoodsOrder.setProjectId(apiReq.getBody().getProjectId());
							supplierGoodsOrder.setProjectName(apiReq.getBody().getProjectName());
							supplierGoodsOrder.setGoodsCode(goods.getId());
							supplierGoodsOrder.setGoodsName(goods.getName());
							supplierGoodsOrder.setSupplierId(supplier.getUserId());
							supplierGoodsOrder.setSupplierName(supplier.getName());
							supplierGoodsOrder.setSupplierTradeNo(tempResp.getBody().getTradeNo());
							supplierGoodsOrder.setMoney(tempResp.getBody().getMoney());
							supplierGoodsOrder.setState(tempResp.getBody().getState());
							supplierGoodsOrder.setCreateDate(new Date());
							if (dao.insert(supplierGoodsOrder) > 0) {
								return tempResp;
							}
						}
					}else{
						return tempResp;
					}
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		} else {
			log.error("供货商{{0}}不存在", goods.getSupplierId());
		}
		return null;
	}

	public boolean updateState(String tradeNo,Integer state){
		if(StringTools.isNotBlank(tradeNo) && state != null){
			CloudGoodsPayOrder updateEntity = new CloudGoodsPayOrder();
			updateEntity.setTradeNo(tradeNo);
			updateEntity.setState(state);

			try {
				return dao.updateNotNull(updateEntity) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}

	public boolean updatePayTradeNo(String tradeNo,String payTradeNo){
		if(StringTools.isNotBlank(tradeNo) && StringTools.isNotBlank(payTradeNo)){
			CloudGoodsPayOrder updateEntity = new CloudGoodsPayOrder();
			updateEntity.setTradeNo(tradeNo);
			updateEntity.setPayTradeNo(payTradeNo);

			try {
				return dao.updateNotNull(updateEntity) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}

	public boolean updateSupplyState(String tradeNo,Integer supplyState){
		if(StringTools.isNotBlank(tradeNo) && supplyState != null){
			CloudGoodsPayOrder updateEntity = new CloudGoodsPayOrder();
			updateEntity.setTradeNo(tradeNo);
			updateEntity.setSupplyState(supplyState);

			try {
				return dao.updateNotNull(updateEntity) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}
}
